package ro.savatech.pg.ui;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

import ro.savatech.pg.flightlog.ConfigurationMem;
import ro.savatech.pg.flightlog.FlightIndexEntry;
import ro.savatech.pg.flightlog.FlightMem;

public class Main {
	private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
	private static final String BIG_DUMP = "big.dump";

	private URL bigURL;

	private Main() {
		this.bigURL = Main.class.getResource(BIG_DUMP);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Main main = new Main();
//		try {
			main.showMainUI();
			//main.showFlight(3);
//		} catch (IOException e) {
//			LOGGER.log(Level.SEVERE, "could not read files.", e);
//			System.exit(2);
//		}
	}

	private void showMainUI() {
		MainFrame frame = new MainFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	private void showFlight(int flightNb) throws IOException {
		ConfigurationMem cfgMem = null;
		FlightIndexEntry[] indexEntries = null;
		FlightMem flightMem = null;

		InputStream inputStream = this.bigURL.openStream();
		try {
			cfgMem = ConfigurationMem.fromStream(inputStream);
			indexEntries = cfgMem.getIndexEntries();
			flightMem = FlightMem.fromStream(indexEntries, inputStream);
		} finally {
			inputStream.close();
		}

		FlightPlot flightPlot = new FlightPlot(flightMem.getFlights()[flightNb]);
		flightPlot.setVisible(true);
	}
}
